/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.attendance.clazz.service.impl;

import com.arisgo.cloud.attendance.clazz.entity.ClassSegment;
import com.arisgo.cloud.attendance.clazz.entity.ClassShift;
import com.arisgo.cloud.attendance.clazz.mapper.ClassSegmentMapper;
import com.arisgo.cloud.attendance.clazz.mapper.ClassShiftMapper;
import com.arisgo.cloud.attendance.clazz.service.ClassShiftService;
import com.arisgo.cloud.attendance.clazz.vo.ClassSegmentVo;
import com.arisgo.cloud.attendance.clazz.vo.ClassShiftVo;
import com.arisgo.cloud.common.Pagination;
import com.arisgo.cloud.common.utils.AssertUtil;
import com.arisgo.cloud.web.model.NumberIdWithTime;
import com.arisgo.cloud.web.mybatis.service.CloudServiceImpl;
import com.arisgo.cloud.web.param.Paging;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 班次
 *
 * @author Coulson
 * @since 2024-12-31
 */
@Slf4j
@Service
@AllArgsConstructor
public class ClassShiftServiceImpl extends CloudServiceImpl<ClassShiftMapper, ClassShift, ClassShiftVo> implements ClassShiftService {

    @Override
    public ClassShift getInstanceModel() {
        return new ClassShift();
    }

    @Override
    public ClassShiftVo getInstanceVo() {
        return new ClassShiftVo();
    }

    private final ClassSegmentMapper classSegmentMapper;

    @Override
    @Transactional
    public ClassShiftVo insert(ClassShiftVo vo) {
        AssertUtil.notNull(vo, "班次为空!");
        AssertUtil.hasLength(vo.getName(), "班次名称为空!");

        ClassShiftVo shift = this.save(vo);
        // 返回班次信息
        shift.setSegments(convert(insertSegments(shift.getId(), vo.getSegments()), ClassSegmentVo.class));
        return shift;
    }

    private List<ClassSegment> insertSegments(Long shiftId, List<ClassSegmentVo> segments) {
        if (CollectionUtils.isEmpty(segments)) {
            return Collections.emptyList();
        }
        // 保存班段
        List<ClassSegment> data = Lists.newArrayListWithCapacity(segments.size());
        for (ClassSegmentVo segment : segments) {
            ClassSegment classSegment = new ClassSegment();
            BeanUtils.copyProperties(segment, classSegment);
            classSegment.setShiftId(shiftId);
            classSegment.setStartTime(segment.getWorkTime().get(0));
            classSegment.setEndTime(segment.getWorkTime().get(1));
            classSegment.setRestStartTime(segment.getRestTime().get(0));
            classSegment.setRestEndTime(segment.getRestTime().get(1));
            data.add(classSegment);
        }
        classSegmentMapper.insertBatch(data);
        return data;
    }

    @Override
    public Boolean edit(ClassShiftVo vo) {
        AssertUtil.notNull(vo, "班次为空!");
        AssertUtil.hasLength(vo.getName(), "班次名称为空!");

        this.update(vo);
        // 删除已有班段
        Map<String, Object> params = Maps.newHashMap();
        params.put(ClassSegment.SHIFT_ID, vo.getId());
        classSegmentMapper.deleteByMap(params);
        // 重新插入班段
        insertSegments(vo.getId(), vo.getSegments());
        return true;
    }

    @Override
    @Transactional
    public Boolean removeShift(Long id) {
        AssertUtil.notNull(id, "班次ID不能为空!");
        // 删除班段
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(ClassSegment.SHIFT_ID, id);
        classSegmentMapper.deleteByQuery(wrapper);
        // 删除班次
        return remove(id);
    }

    @Override
    @Transactional
    public Boolean removeShift(List<Long> ids) {
        AssertUtil.notEmpty(ids, "班次ID不能为空!");
        // 删除班段
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in(ClassSegment.SHIFT_ID, ids);
        classSegmentMapper.deleteByQuery(wrapper);
        // 删除班次
        return remove(ids);
    }

    @Override
    @Transactional
    public Boolean deleteShift(Long id) {
        AssertUtil.notNull(id, "班次ID不能为空!");
        // 删除班段
        UpdateChain.of(new ClassSegment())
                .set(NumberIdWithTime.IS_DELETED, 1)
                .eq(ClassSegment.SHIFT_ID, id).update();
        // 删除班次
        return delete(id);
    }

    @Override
    @Transactional
    public Boolean deleteShift(List<Long> ids) {
        AssertUtil.notEmpty(ids, "班次ID不能为空!");
        // 删除班段
        classSegmentMapper.deleteByShiftIds(ids);
        // 删除班次
        return delete(ids);
    }

}
